//
//  SYJEmoticonPageViewCell.swift
//  weibo
//
//  Created by 孙英建 on 16/4/25.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class SYJEmoticonPageViewCell: UICollectionViewCell {
    
    // 表情一维数组
    var emoticonArr: [SYJEmoticonModel]?{
        didSet{
            
            for button in buttonArr{
                button.hidden = true
            }
            
            for (i, emoticon) in emoticonArr!.enumerate(){
                
                let button = buttonArr[i]
                button.hidden = false
                
                if emoticon.isEmoji{
                    // emoji表情
                    button.setTitle(((emoticon.code ?? "") as NSString).emoji(), forState: UIControlState.Normal)
                    button.setImage(nil, forState: UIControlState.Normal)
                }else{
                    // 图片表情
                    let path = "\(emoticon.path!)\(emoticon.png!)"
                    
                    let image = UIImage(named: path, inBundle: SYJEmoticonTools.sharedTools.emoticonBundle, compatibleWithTraitCollection: nil)
                    
                    button.setImage(image, forState: UIControlState.Normal)
                    // 解决cell重用问题
                    button.setTitle(nil, forState: UIControlState.Normal)

                    
                }
                
            }
        }
    }

    
    // 记录表情按钮
    var buttonArr: [UIButton] = [UIButton]()
    
    
    
    var indexPath: NSIndexPath?{
        didSet{
//            countLabel.text = "\(indexPath?.section ?? 0)--\(indexPath?.item ?? 0)"
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 设置控件
    func setupUI(){
        
        self.backgroundColor = UIColor.whiteColor()
        
//        contentView.addSubview(countLabel)
        
//        countLabel.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(contentView)
//        }
        
        addChildButton()
        
        // 删除按钮
        contentView.addSubview(deleteButton)
    }
    
    // 设置frame
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = (kWidth - 10) / CGFloat(emoticonMaxCol)
        let h = (216 - 37 - 20) / CGFloat(emoticonMaxRow)
        
        // 循环按钮设置frame
        for (i, button) in buttonArr.enumerate(){
            let x = CGFloat(i % emoticonMaxCol) * w + 5
            let y = CGFloat(i / emoticonMaxCol) * h
            
            button.frame = CGRect(x: x, y: y, width: w, height: h)
            
        }
        
        // 删除按钮
        deleteButton.frame = CGRect(x: kWidth - 5 - w, y: h * 2, width: w, height: h)
    }
    
    
    // 添加表情按钮
    func addChildButton(){
        
        for _ in 0..<emoticonMaxCount{
            let btn = UIButton()
            btn.titleLabel?.font = UIFont.systemFontOfSize(32)
            contentView.addSubview(btn)
            buttonArr.append(btn)
        }
    }
    
    
    // MARK: - 懒加载控件
    // 显示位置的label
//    private lazy var countLabel: UILabel = {
//        let lab = UILabel()
//        lab.font = UIFont.systemFontOfSize(35)
//        lab.textColor = UIColor.redColor()
//        lab.textAlignment = .Center
//        return lab
//    }()
    
    // 删除按钮
    private lazy var deleteButton: UIButton = {
       let deleteBtn = UIButton()
        deleteBtn.setImage(UIImage(named: "compose_emotion_delete"), forState: UIControlState.Normal)
        deleteBtn.setImage(UIImage(named: "compose_emotion_delete_highlighted"), forState: UIControlState.Highlighted)
        return deleteBtn
    }()
    
    
    
}








